package patterns;

public class LearnAdapter {

    // 主方法测试
    public static void main(String[] args) {
        LearnAdapter outer = new LearnAdapter();
        OldPlayer player = new OldPlayer();

        player.play("mp3", "song.mp3");  // ✅ 原生支持
        player.play("mp4", "video.mp4"); // ✅ 通过适配器支持
        player.play("avi", "movie.avi"); // ❌ 不支持
    }
}

// 1️⃣ 客户端期待的接口
interface MediaPlayer {
    void play(String audioType, String fileName);
}

// 2️⃣ 高级播放器（已有功能，但接口不同）
class AdvancedPlayer {
    public void playMp4(String fileName) {
        System.out.println("Playing mp4 file: " + fileName);
    }
}

// 3️⃣ 适配器：将 AdvancedPlayer 适配成 MediaPlayer 接口
class MediaAdapter implements MediaPlayer {
    private AdvancedPlayer advancedPlayer;

    public MediaAdapter() {
        advancedPlayer = new AdvancedPlayer();
    }

    @Override
    public void play(String audioType, String fileName) {
        if (audioType.equalsIgnoreCase("mp4")) {
            advancedPlayer.playMp4(fileName);
        } else {
            System.out.println("Unsupported format: " + audioType);
        }
    }
}

// 4️⃣ 老播放器（只能播 mp3）
class OldPlayer implements MediaPlayer {
    private MediaAdapter adapter;

    @Override
    public void play(String audioType, String fileName) {
        if (audioType.equalsIgnoreCase("mp3")) {
            System.out.println("Playing mp3 file: " + fileName);
        } else if (audioType.equalsIgnoreCase("mp4")) {
            adapter = new MediaAdapter(); // 使用适配器处理 mp4
            adapter.play(audioType, fileName);
        } else {
            System.out.println("Invalid media type: " + audioType);
        }
    }
}

